package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.mapper.ZjNetworkAloneAuthenFieldValMapper;
import com.zb.service.ZjNetworkAloneAuthenFieldService;
import com.zb.service.ZjNetworkAloneAuthenFieldValOptionService;
import com.zb.service.ZjNetworkAloneAuthenFieldValService;
import com.zb.vo.NetworkFieldValVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 网点个人认证字段值表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-01
 */
@Service
public class ZjNetworkAloneAuthenFieldValServiceImpl extends ServiceImpl<ZjNetworkAloneAuthenFieldValMapper, ZjNetworkAloneAuthenFieldVal> implements ZjNetworkAloneAuthenFieldValService {
    @Resource
    private ZjNetworkAloneAuthenFieldService networkAloneAuthenFieldService;
    @Resource
    private ZjNetworkAloneAuthenFieldValOptionService networkAloneAuthenFieldValOptionService;

    // 保存个人服务商认证字段
    @Override
    public void save(Long custAccId, Long networkId, List<NetworkFieldValVo> fieldVals) {
        if (CollectionUtil.isEmpty(fieldVals)) {
            return;
        }
        List<ZjNetworkAloneAuthenFieldVal> adds = new ArrayList<>();
        List<ZjNetworkAloneAuthenFieldVal> updates = new ArrayList<>();
        ZjNetworkAloneAuthenFieldVal val = null;
        List<ZjNetworkAloneAuthenFieldValOption> optionList = new ArrayList<>();
        ZjNetworkAloneAuthenFieldValOption opt = null;
        for (NetworkFieldValVo vo : fieldVals) {
            val = new ZjNetworkAloneAuthenFieldVal();
            BeanUtil.copyProperties(vo, val);
            if (null != val.getId()) {
                updates.add(val);
            } else {
                val.setId(IdWorker.getId());
                val.setCustAccId(custAccId);
                val.setNetworkId(networkId);
                val.setNetworkAloneAuthenFieldId(vo.getFieldId());
                adds.add(val);
            }
            for (FieldValOptionVo optVo : vo.getOptVos()) {
                opt = new ZjNetworkAloneAuthenFieldValOption();
                opt.setId(IdWorker.getId());
                opt.setCustAccId(custAccId);
                opt.setNetworkId(networkId);
                opt.setNetworkAloneAuthenFieldValId(val.getId());
                opt.setOptValue(optVo.getOptValue());
                optionList.add(opt);
            }
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
        networkAloneAuthenFieldValOptionService.deleteByNetworkId(networkId);
        networkAloneAuthenFieldValOptionService.saveBatch(optionList);
    }

    // 根据服务商ID获取个人服务商认证信息
    @Override
    public List<FieldValVo> getByNetworkId(Long networkId) {
        QueryWrapper<ZjNetworkAloneAuthenFieldVal> wrapper = new QueryWrapper<>();
        wrapper.select("id", "network_alone_authen_field_id", "type")
                .eq("network_id", networkId);
        List<ZjNetworkAloneAuthenFieldVal> fieldValList = this.list(wrapper);
        if (CollectionUtil.isEmpty(fieldValList)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkAloneAuthenField> aloneAuthenFieldQueryWrapper = new QueryWrapper<>();
        aloneAuthenFieldQueryWrapper.select("id", "name")
                .in("id", fieldValList.stream().map(ZjNetworkAloneAuthenFieldVal::getNetworkAloneAuthenFieldId).toList());
        List<ZjNetworkAloneAuthenField> authenFieldList = networkAloneAuthenFieldService.list(aloneAuthenFieldQueryWrapper);

        List<ZjNetworkAloneAuthenFieldValOption> optionList = networkAloneAuthenFieldValOptionService.getByNetworkId(networkId);
        List<FieldValVo> voList = fieldValList.stream().map(val -> {
            FieldValVo vo = new FieldValVo();
            vo.setId(val.getId());
            vo.setType(val.getType());
            authenFieldList.stream().filter(f -> Objects.equals(f.getId(), val.getNetworkAloneAuthenFieldId())).findFirst()
                    .map(ZjNetworkAloneAuthenField::getName).ifPresent(vo::setName);
            vo.setOptVos(optionList.stream().filter(f -> Objects.equals(f.getNetworkAloneAuthenFieldValId(), val.getId())).map(opt -> {
                return new FieldValOptionVo(opt.getId(), opt.getOptValue());
            }).toList());
            return vo;
        }).toList();
        return voList;
    }
}
